using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{

    public partial class Form1 : Form
    {
        string[] comboBoxPorts;
        //static public ArrayList listBoxDataSource = new ArrayList();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxPorts = Program.scanPorts();
            int indexOfComPort = Program.discoverComPort();
            comPortComboBox.DataSource = comboBoxPorts;
            comPortComboBox.SelectedIndex = indexOfComPort;
            if (indexOfComPort > -1 && string.Compare(comPortComboBox.SelectedValue.ToString(), Program.ports[indexOfComPort]) == 0)
            {
                comPortComboBox.BackColor = Color.PaleGreen;
                listBox1.Items.Add( "Successfully discovered Ambient Trust on " + Program.ports[indexOfComPort] );
                //listBoxDataSource.Add("Successfully discovered Ambient Trust on " + Program.ports[indexOfComPort]);
                //listBox1.DataSource = listBoxDataSource;


            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            comboBoxPorts = Program.scanPorts();
            comPortComboBox.DataSource = comboBoxPorts;
            int indexOfComPort = Program.discoverComPort();
            comPortComboBox.SelectedIndex = Program.discoverComPort();
            if (indexOfComPort > -1 && string.Compare(comPortComboBox.SelectedValue.ToString(), Program.ports[indexOfComPort]) == 0)
                comPortComboBox.BackColor = Color.PaleGreen;
        }

        private void comPortComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public delegate void updateListBoxDelegate(string updateWith);

        static public void updateListBox(string updateWith)
        {
         //   Form1.listBoxDataSource.Add(updateWith);
           // listBox1.DataSource = Form1.listBoxDataSource;
            if (listBox1.InvokeRequired)
            {
                // This is a worker thread so delegate the task.
                listBox1.BeginInvoke(new updateListBoxDelegate(updateListBox), updateWith);
            }
            else
            {
                // This is the UI thread so perform the task.
                listBox1.Items.Add(updateWith);
                listBox1.TopIndex = listBox1.Items.Count - 1;
            }

        }

        private void OnKeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                Program.send(textBox1.Text);
                textBox1.Text = "";
            }
        }



    }
}